package com.recruit.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.recruit.common.mybatis.Page;
import com.recruit.common.util.PageUtil;
import com.recruit.dto.advertising.CreateOrUpdateAdvertisingDTO;
import com.recruit.model.AdvertisingDO;
import com.recruit.module.file.FileProperties;
import com.recruit.service.AdvertisingService;
import com.recruit.vo.*;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.core.annotation.GroupRequired;
import io.github.talelin.core.annotation.Logger;
import io.github.talelin.core.annotation.PermissionMeta;
import io.github.talelin.core.annotation.PermissionModule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.Positive;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/recruit/advertising")
@PermissionModule(value = "首页广告图片管理")
public class AdvertisingController {

    @Resource
    private AdvertisingService advertisingService;

    @Value("${index.adv-pics-count}")
    private Integer advPicsCount = 6;

    @Autowired
    private FileProperties fileProperties;

    /**
     * 新增广告图片
     * @return
     */
    @Logger(template = "新增广告图片")
    @GroupRequired
    @PermissionMeta(value = "新增广告图片")
    @PostMapping("")
    public CreatedVO create(@RequestBody @Validated CreateOrUpdateAdvertisingDTO validator) {
        AdvertisingDO advDo = new AdvertisingDO();
        BeanUtil.copyProperties(validator,advDo);
        advertisingService.saveOrUpdate(advDo);
        return new CreatedVO(8500);
    }

    /**
     * 根更新首页轮播广告图片的优先级跟状态
     *
     * @param id
     * @param validator
     * @return
     */
    @Logger(template = "更新首页轮播广告图片的优先级跟状态")
    @PutMapping("/{id}")
    @GroupRequired
    @PermissionMeta(value = "更新首页轮播广告图片的优先级跟状态")
    public UpdatedVO update(@PathVariable("id") @Positive(message = "{id.positive}") Integer id,
                            @RequestBody @Validated CreateOrUpdateAdvertisingDTO validator) {
        AdvertisingDO advDo = advertisingService.getById(id);
        if (advDo == null) {
            throw new NotFoundException(8530);
        }
        BeanUtil.copyProperties(validator,advDo);
        advDo.setUpdateTime(new Date());
        advertisingService.saveOrUpdate(advDo);
        return new UpdatedVO(8510);
    }

    /**
     * 根据id删除广告图片
     * @param id
     * @return
     */
    @Logger(template = "删除广告图片")
    @DeleteMapping("/{id}")
    @GroupRequired
    @PermissionMeta(value = "删除新闻资讯")
    public DeletedVO delete(@PathVariable @Positive(message = "{id.positive}") Integer id) {
        AdvertisingDO advDO = advertisingService.getById(id);
        if (advDO == null) {
            throw new NotFoundException(8530);
        }
        advertisingService.removeById(advDO.getId());
        advDO.setUpdateTime(new Date());
        return new DeletedVO(8520);
    }

    /**
     * 首页轮播广告图片
     *
     * @return
     */
    @GetMapping("/advPics")
    public ResponseVO<List<String>> indexPageAdvPic(){
        ResponseVO<List<String>> vo = new ResponseVO<>();
        vo.setCode(HttpStatus.OK.value());
        //根据优先级、最新修改日期降序取6个url
        List<AdvertisingDO> list = advertisingService.advPics(advPicsCount);
        if(list == null || list.isEmpty()){
             vo.setData(new ArrayList<String>(){{add(fileProperties.getDomain() + "assets/index/1647940128.jpg");}});
        }else{
            vo.setData(list.stream().map(AdvertisingDO::getUrl).collect(Collectors.toList()));
        }
        return vo;
    }

    @GetMapping("/page")
    public PageResponseVO<AdvertisingDO> page(@RequestParam(name = "count", required = false, defaultValue = "10")
                                                  @Min(value = 1, message = "{page.count.min}")
                                                  @Max(value = 30, message = "{page.count.max}") Integer count,
                                              @RequestParam(name = "page", required = false, defaultValue = "0")
                                                  @Min(value = 0, message = "{page.number.min}") Integer page){
        Page<AdvertisingDO> pages = new Page<>(page,count);
        QueryWrapper<AdvertisingDO> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("update_time");
        IPage<AdvertisingDO> iPage = advertisingService.page(pages,wrapper);
        return PageUtil.build(iPage);
    }

    @GetMapping("/get/{id}")
    public AdvertisingDO get(@PathVariable(value = "id") @Positive(message = "{id.positive}") Integer id){
        AdvertisingDO advertisingDO = advertisingService.getById(id);
        if(advertisingDO == null){
            throw new NotFoundException(8530);
        }
        return advertisingDO;
    }

}
